import { dateFormat, dayDiff } from '@/utils/formatDate.js'

export default {
  data() {
    return {
      type: '', //0 新增 1 编辑
      id: '',
      successPoup: false,
      info: {},
      remainder: '', //剩余时间
      freightPrice: '', //运费
      contactId: '', //联系人ID
      contactName: '', //联系人
      chooserTime: '', //页面显示有效期
      effectiveTime: '', //接口需要的有效期
      dateRage: [
        {
          value: 3,
          label: '3天',
        },
        {
          value: 5,
          label: '5天',
        },
        {
          value: 7,
          label: '7天',
        },
        {
          value: 15,
          label: '15天',
        },
        {
          value: 30,
          label: '30天',
        },
      ],
      remarks: '',
      contactIndex: '',
      pageLoading: false, //初始化加载效果
    }
  },

  computed: {
    formatName() {
      if (this.info.trueName) {
        return (
          this.info.trueName.slice(0, 1) +
          '*'.repeat(this.info.trueName.length - 1)
        )
      }
      return ''
    },
    formatPhone() {
      if (this.info.store_Phone) {
        return this.info.store_Phone.replace(/(.{3}).*(.{4})/, '$1****$2')
      }
      return ''
    },
    statusIcon() {
      return this.$store.state.accessory.statusIcon
    },
    status() {
      return this.$store.state.accessory.status
    },
    quality() {
      return this.$store.state.accessory.quality
    },
  },

  onLoad(e) {
    this.type = e.type
    this.id = e.id
    if (this.type === '1') {
      uni.setNavigationBarTitle({
        title: '编辑报价',
      })
    }
    this.getInfo(e.id)
    this.getContact()

    uni.$on('selContact', (res) => {
      this.contactId = res.id
      this.contactName = res.name
      this.contactIndex = res.index
    })
  },

  methods: {
    dateFormat,
    dayDiff,

    //获取订单信息
    getInfo(id) {
      // uni.showLoading()
      this.$api.accessory
        .getCarPartOfferByCarPartId({
          id: id,
        })
        .then((res) => {
          this.info = res.data
          this.remainder = this.dayDiff(this.info.effective_Time)
          if (this.info.carPartOffer) {
            //有效期时间处理
            this.freightPrice = this.info.carPartOffer.freight_price
            this.remarks = this.info.carPartOffer.remarks
          }
          if (this.type === '1') {
            //编辑报价进入
            this.chooserTime = this.dateFormat(this.info.effective_Time)
            this.effectiveTime = this.info.effective_Time
          }
          this.pageLoading = true
        })
        .catch((e) => {
          console.log(e)
          // uni.hideLoading()
          this.pageLoading = true
        })
    },

    //获取默认联系人
    getContact() {
      this.$api.common
        .getContactList({
          pageIndex: this.pageIndex,
          pageSize: 1,
        })
        .then((res) => {
          if (res.data.total !== 0) {
            this.contactId = res.data.list[0].id
            this.contactName = res.data.list[0].name
            this.contactIndex = 0
          }
        })
        .catch((e) => {})
    },
    //选择有效期
    selDate() {
      uni.showActionSheet({
        itemList: ['3天', '5天', '7天', '15天', '30天'],
        success: (res) => {
          this.chooserTime = this.dateRage[res.tapIndex].label
          this.handleEffectiveTime(res.tapIndex)
        },
        fail: function (res) {
          console.log(res.errMsg)
        },
      })
    },

    // 选择联系人
    selContact() {
      uni.navigateTo({
        url:
          '/pages_common/chooseContact/chooseContact?contactIndex=' +
          this.contactIndex,
      })
    },

    //品质选择
    selectQuality(type, index) {
      this.info.carPartDetails[index].character_type = type
    },

    submit() {
      let carPartOfferDetails = []
      let flag = false
      for (let i = 0; i < this.info.carPartDetails.length; i++) {
        if (
          this.info.carPartDetails[i].offer_price !== '' &&
          this.info.carPartDetails[i].offer_price > 0
        ) {
          flag = true
        } else {
          this.info.carPartDetails[i].offer_price = '0'
        }

        carPartOfferDetails.push({
          id:
            this.type === '0'
              ? ''
              : this.info.carPartDetails[i].car_part_offer_details_id,
          car_part_details_id: this.info.carPartDetails[i].id,
          offer_price: this.info.carPartDetails[i].offer_price,
          character_type: this.info.carPartDetails[i].character_type,
        })
      }
      if (!flag) {
        uni.showToast({
          title: '至少选择一个配件进行报价',
          icon: 'none',
        })
        return
      }
      if (!this.contactId) {
        uni.showToast({
          title: '请选择报价联系人',
          icon: 'none',
        })
        return
      }
      if (!this.effectiveTime) {
        uni.showToast({
          title: '请选择报价有效期',
          icon: 'none',
        })
        return
      }
      this.$api.accessory
        .submitQuote({
          id: this.type === '0' ? '' : this.id, //编辑时候的报价id
          car_Part_Id: this.id, //询件列表进入的id
          car_Quoted_Contact_B_Id: this.contactId,
          freight_Price: this.freightPrice ? this.freightPrice : 0,
          effective_Time: this.effectiveTime,
          remarks: this.remarks,
          carPartOfferDetails: carPartOfferDetails,
        })
        .then((res) => {
          console.log(res)
          if (res.data) {
            this.successPoup = true
          } else {
            uni.showToast({
              title: '报价失败，请稍后重试',
              icon: 'none',
            })
          }
        })
        .catch((e) => {
          console.log(e)
        })
    },

    //图片处理
    handdleImg(imgStr) {
      if (imgStr) {
        return JSON.parse(imgStr)
      }
    },

    //输入金额验证
    verifyImput(e, i) {
      let value = e.detail.value
      // if(value.indexOf('.') == 0){ //.开始前面补0
      // 	value = '0' + e.detail.value;
      // } else if(value.indexOf('.') === value.length-1){ //.结尾删除.
      // 	value = e.detail.value.slice(0,-1)
      // 	this.info.carPartDetails[i].offer_price = Number(value)
      // }
      let valueArr = value.split('.')
      if (valueArr.length > 2) {
        //有多个小数点
        uni.showTost({
          title: '金额输入错误，请重新输入',
          icon: 'none',
        })
        this.info.carPartDetails[i].offer_price = 0
      } else if (valueArr.length == 2) {
        //有小数点
        if (valueArr[1].length > 2) {
          //小数点后大于2位,仅保留2位
          value = valueArr[0] + '.' + valueArr[1].slice(0, 2)
          this.info.carPartDetails[i].offer_price = Number(value)
        }
      }
    },

    //运费验证
    verifyFreight(e) {
      let value = e.detail.value
      // if(value.indexOf('.') == 0){ //.开始前面补0
      // 	value = '0' + e.detail.value;
      // } else if(value.indexOf('.') === value.length-1){ //.结尾删除.
      // 	value = e.detail.value.slice(0,-1)
      // 	this.info.carPartDetails[i].offer_price = Number(value)
      // }
      let valueArr = value.split('.')
      if (valueArr.length > 2) {
        //有多个小数点
        uni.showTost({
          title: '金额输入错误，请重新输入',
          icon: 'none',
        })
        this.freightPrice = 0
      } else if (valueArr.length == 2) {
        //有小数点
        if (valueArr[1].length > 2) {
          //小数点后大于2位,仅保留2位
          value = valueArr[0] + '.' + valueArr[1].slice(0, 2)
          this.freightPrice = Number(value)
        }
      }
    },

    handleEffectiveTime(index) {
      const days = this.dateRage[index].value
      this.effectiveTime = new Date(
        new Date(new Date().toLocaleDateString()).getTime() + days * 86400000
      )
    },

    closPoupe() {
      this.successPoup = false
      uni.navigateBack()
    },
  },
}
